//	添加
$(function(){
	var E=false,N=false;
	//当各项验证不能通过时，按钮点击无效
	$("#btnCreate").click(function(){
		if(E && N){
		}else{
			return false;
		}
		var departmentName = $("#Name").val();
		var description = $("#Description").val();
		$.ajax({
			type:"POST",
			url:"SystemConfig",
			dataType:"json",
			contentType:"application/x-www-form-urlencoded; charset=utf-8",
			async:false,
			data:"method=2&departmentName="+departmentName+"&description="+description,
			success:function(data){
				location.reload();
				alert("添加成功");
			},
			error:function(){
				alert("添加失败");
			}
		});
		
	});
	
	$("#btnReset").click(function(){
		$("#Name").val("");
		$("#Description").val("");
	});
	
//	输入验证
	$("#Name").blur(function() {
		E = false;
		var val = $(this).val();
		var info = $("#name\\.info");
		info.html("");
		if (val == "") {
			info.text("部门名称不能为空");
		} else if (!val.match(/^[a-z0-9\u4e00-\u9efa5]{1,20}$/)) {
			info.text("部门名称格式不正确");
		} else {
			E = true;
			info.html("<img src='./images/ajax_ok.png' />");
		}
	});
	
	$("#Description").blur(function() {
		N = false;
		var val = $(this).val();
		var info = $("#description\\.info");
		info.html("");
		if (val == "") {
			info.text("部门描述不能为空");
		} else if (!val.match(/^[a-z0-9\u4e00-\u9efa5]{1,20}$/)) {
			info.text("部门描述格式不正确");
		} else {
			N = true;
			info.html("<img src='./images/ajax_ok.png' />");
		}
	});
	
});

//	修改
$(function(){
	$(".alter").click(function(){
		var departmentId = $(this).parent().prev().prev().attr("deparmetId");
		$("#Name1").val($(this).parent().prev().prev().html());
		$("#Description1").val($(this).parent().prev().html());
		$("#btnCreate1").click(function(){
			var departmentName = $("#Name1").val();
			var description = $("#Description1").val();
			$.ajax({
				type:"POST",
				url:"SystemConfig",
				dataType:"json",
				contentType:"application/x-www-form-urlencoded; charset=utf-8",
				async:false,
				data:"method=3&departmentName="+departmentName+"&description="+description+"&departmentId="+departmentId,
				success:function(data){
					location.reload();
					alert("修改成功");
				},
				error:function(){
					alert("修改失败");
				}
			});
		});
	});
	$("#btnReset1").click(function(){
		$("#Name1").val("");
		$("#Description1").val("");
	});
});
